//
//  MyCounterCell.m
//  ConnectorDemo
//
//  Created by huck on 2023/3/28.
//  Copyright © 2023 huck. All rights reserved.
//

#import "MyCounterCell.h"

@interface MyCounterCell()<UITextFieldDelegate>

@end

@implementation MyCounterCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.lots.delegate = self;
    self.openPrice.delegate = self;
    self.closePrice.delegate = self;
    // Configure the view for the selected state
}

-(void)refreshCell:(Counter *)info row:(NSIndexPath*)indexPath currentPrice:(NSString *)currentPrice {
    Boolean buy = indexPath.section == 0;
    self.typeLabel.text = [NSString stringWithFormat:@"%@%li",buy ? @"多":@"空",indexPath.row+1];
    self.lots.text = info.lots;
    self.openPrice.text = info.openPrice;
    self.closePrice.text = info.closePrice;
    self.point.text = nil;
        
    if ([Util isNotEmpty:info.lots] && [Util isNotEmpty:info.openPrice]) {
        CGFloat open = info.openPrice.floatValue;
        CGFloat point = 0;
        if ([Util isNotEmpty:info.closePrice]) {
            CGFloat close = info.closePrice.floatValue;
            point = buy?(close-open):(open-close);
            self.point.text = [Util floatToString:point];
        } else if ([Util isNotEmpty:currentPrice]) {
            CGFloat current = currentPrice.floatValue;
            point = buy?(current-open):(open-current);
            self.point.text = [Util floatToString:point];
        }
        self.point.textColor = point >= 0 ? [UIColor redColor] : [UIColor greenColor];
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason API_AVAILABLE(ios(10.0)) {
    Counter *info  = [[Counter alloc] initWithOpen:self.openPrice.text close:self.closePrice.text lots:self.lots.text];
    if (self.editFinishBlock) {
        self.editFinishBlock(info);
    }
}

@end
